<?php
namespace app\api\controller;

use app\api\controller\Sample;
use app\common\controller\ApiController;
use think\App;
use \app\admin\traits\Curd;
use think\facade\Db;
use app\api\controller\Adv;
use app\api\model\User as ModelUser;
use Exception;
use PhpOffice\PhpSpreadsheet\Reader\Xls\MD5;

class TestApi extends ApiController
{
    protected $noNeedLogin = [
        "TestApiPost3003",
         
    ];
    protected $mustLogin = [];
    protected $counter = 0;

    use \app\admin\traits\Curd;
    public function __construct(App $app)
    {
        parent::__construct($app);
    }
  
    //根据车架号照片,获取车辆信息
    public function TestApiPost3003()
    {
        $local_imgpath = "http://www.17vin.com/images/vin_test.png";//"http://www.17vin.com/images/vin_test.png"; http://api.cxyc.fun/cjh.png  
        $user = "chuangling"; // 这里更改您的用户名
        $pass = "ks39dw"; // 这里更改您的密码

        $action = "vin_ocr_and_vin_decode";
        $imageBytes = file_get_contents($local_imgpath);
        $base64String = base64_encode($imageBytes);
        $base64String_urlencode = urlencode($base64String);
        $url_parameters = "/?action=" . $action . "&base64_urlencode_imagestring=" . $base64String_urlencode;
        $token = self::getToken($user, $pass, $url_parameters);

        $baseUrl = "http://api.17vin.com:8080";
        // 调用函数提交表单
        $response = self::submitForm($baseUrl, $action, $base64String_urlencode, $user, $token);
        // 输出响应内容
        $this->success("获取成功",json_decode($response,true));
       
    }

    public static function submitForm($baseUrl, $action, $base64String_urlencode, $user, $token)
    {
        // 创建HTTP上下文选项
        $options = [
            'http' => [
                'header'  => "Content-Type: application/x-www-form-urlencoded\r\n",
                'method'  => 'POST',
                'content' => http_build_query([
                    'action' => $action,
                    'base64_urlencode_imagestring' => $base64String_urlencode,
                    'user' => $user,
                    'token' => $token,
                ]),
            ],
        ];
        $context  = stream_context_create($options);
        $result = file_get_contents($baseUrl, false, $context);

        if ($result === FALSE) {
            // 处理错误
            throw new Exception("Error occurred while submitting the form.");
        }

        return $result;
    }

    private static function getMd5($convertString)
    {
        return md5($convertString);
    }

    private static function getToken($user, $pass, $url_parameters)
    {
        return self::getMd5(self::getMd5($user) . self::getMd5($pass) . $url_parameters);
    }
 
   

    
 
   
}
